﻿using Collaboration_System.DataBase;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows.Documents;
using System.Windows.Controls;
using static MaterialDesignThemes.Wpf.Theme;
using System;
using System.Windows.Threading;

namespace Collaboration_System
{
    /// <summary>
    /// Логика взаимодействия для Dashboard.xaml
    /// </summary>
    public partial class Dashboard : Window
    {
        Collaboration_SystemEntities entity = new Collaboration_SystemEntities();
        public List<Notes> notes;
        public List<Users> users;
        private string path;

        public Dashboard()
        {
            InitializeComponent();

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string idUserString = File.ReadLines("config.json").Skip(1).First();
            UserName.Text = idUserString;
            string idRoleString = File.ReadLines("config.json").Skip(2).First();
            Role.Text = idRoleString;
            string idString = File.ReadLines("config.json").Skip(0).First();
            path = idString;
            if (!File.Exists(path + ".txt"))
            {
                File.Create(path + ".txt");
            }
            var timer = new DispatcherTimer
            { Interval = TimeSpan.FromSeconds(1) };
            timer.Start();
            timer.Tick += (sender1, args) =>
            {
                timer.Stop();
                if (File.Exists(path + ".txt"))
                {
                    NoteList.Text = File.ReadAllText(path + ".txt");
                }
            };
            var userData = entity.Users.FirstOrDefault(u => u.login == idUserString);
            TaskCount.Text = userData.TaskCount.ToString();
            CompletedTask.Text = userData.CompletedTasks.ToString();
            SuccTask.Text = userData.SuccessfulTasks.ToString();
            FailedTask.Text = userData.FailedTasks.ToString();

            double progressbar1 = (Convert.ToDouble(TaskCount.Text)/80) * 100;
            ProgressBar1.Value = progressbar1 ;
            double progressbar2 = (Convert.ToDouble(CompletedTask.Text) / 80) * 100;
            ProgressBar2.Value = progressbar2;
            double progressbar3 = (Convert.ToDouble(SuccTask.Text) / 80) * 100;
            ProgressBar3.Value = progressbar3;
            double progressbar4 = (Convert.ToDouble(FailedTask.Text) / 80) * 100;
            ProgressBar4.Value = progressbar4;

        }

        private void Task_Click(object sender, RoutedEventArgs e)
        {
            TaskWindow autorization = new TaskWindow();
            autorization.Show();
            Close();
        }

        private void Lids_Click(object sender, RoutedEventArgs e)
        {
            LidsWindow autorization = new LidsWindow();
            autorization.Show();
            Close();
        }

        private void Deal_Click(object sender, RoutedEventArgs e)
        {
            TransactionsWindow autorization = new TransactionsWindow();
            autorization.Show();
            Close();
        }

        private void Company_Click(object sender, RoutedEventArgs e)
        {
            CompanyWindow autorization = new CompanyWindow();
            autorization.Show();
            Close();
        }

        private void Product_Click(object sender, RoutedEventArgs e)
        {
            ProductWindow autorization = new ProductWindow();
            autorization.Show();
            Close();
        }

        private void ExitAkk_Click(object sender, RoutedEventArgs e)
        {
            File.Delete("config.json");
            Autorization autorization = new Autorization();
            autorization.Show();
            Close();
        }
        private void Home_Click(object sender, RoutedEventArgs e)
        {
            Dashboard autorization = new Dashboard();
            autorization.Show();
            Close();
        }

        private void ClouseButton_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void DeleteRedactButton_Click(object sender, RoutedEventArgs e)
        {
            NoteList.Clear();
            File.WriteAllText(path + ".txt", NoteList.Text);
            MessageBox.Show("Данные очищены");
        }

        private void RedactButton_Click(object sender, RoutedEventArgs e)
        {
            File.WriteAllText(path + ".txt", NoteList.Text);
            MessageBox.Show("Данные сохранены");
        }

      
    }
}
